#include"Queue.h"


Queue::Queue()
{
	_data = 0;
	_next = nullptr;
}

void Queue::PushData(DataType x)
{
	_data = x;
}


void Queue::PushNext(Queue* x)
{
	_next = x;
}


void Queue::Print()
{
	cout << _data << " ";
}

Queue* Queue::Exgange()
{
	return _next;
}

DataType Queue::ReturnData()
{
	return _data;
}


Qu::Qu()
{
	_head = nullptr;
	_tail = nullptr;
}

void Qu::PushBack(DataType x)
{
	Queue* newnode = (Queue*)malloc(sizeof(Queue));
	newnode->PushData(x);
	if (_tail == nullptr && _head == nullptr)
	{
		_tail = _head = newnode;
		newnode->PushNext(nullptr);
		return;
	}
	_tail->PushNext(newnode);
	_tail = newnode;
	_tail->PushNext(nullptr);
}

void Qu::Print()
{
	Queue* temp = _head;
	while (temp!=nullptr)
	{
		temp->Print();
		temp = temp->Exgange();
	}
	cout << endl;
}


void Qu::PopBack()
{
	assert(_head);
	if (_head == _tail)
	{
		free(_head);
		_head = nullptr;
		_tail = nullptr;
		return;
	}
	Queue* temp = _head;
	while (temp->Exgange()!=_tail)
	{
		temp = temp->Exgange();
	}
	free(_tail);
	_tail = temp;
	temp->PushNext(nullptr);
	temp = NULL;
}


Queue* Qu::Find(DataType x)
{
	assert(_head);
	Queue* temp = _head;
	while (temp)
	{
		if (temp->ReturnData() == x)
		{
			return temp;
		}
		temp = temp->Exgange();
	}
	return nullptr;
}


Qu::~Qu()
{
	while (_head)
	{
		Queue *temp= _head->Exgange();
		free(_head);
		_head = NULL;
		_head = temp;
	}
}

void Qu::PushFront(DataType x)
{
	Queue* newnode = (Queue*)malloc(sizeof(Queue));
	newnode->PushData(x);
	if (_tail == nullptr && _head == nullptr)
	{
		_tail = _head = newnode;
		newnode->PushNext(nullptr);
		return;
	}
	newnode->PushNext(_head);
	_head = newnode;
}


void Qu::PopFront()
{
	assert(_head);
	Queue* temp = _head->Exgange();
	free(_head);
	_head = NULL;
	_head = temp;
}